/**
 * User: zhangqiang
 * Date: 15-8-11
 * Time: 上午11:04
 * 查询面板
 */

define(["require",
		"dojo/_base/declare",
		"dojo/_base/lang",
		"dojo/_base/array",
		"dojo/_base/fx",
		"dojo/fx/easing",
		"../../util/ajaxUtil",
		"dojo/dom",
		"dojo/dom-attr",
		"dojo/dom-class",
		"dojo/dom-style",
		"dojo/on",
		"dojo/query",
		"dojo/topic",
		"../BaseWidget",
		"gistech/widgets/LandManageAndControlReportWidget/LandManageAndControlReportWidget",
		 "gistech/widgets/drawGeometryWidget/DrawGeometryWidget",
		"gistech/widgets/SpecialDataReportWidget/SpecialDataReportWidget",
		"esri/layers/FeatureLayer","esri/InfoTemplate",
		"dijit/layout/ContentPane","dojo/text!./templates/Template.html"],
    function (require, declare, lang, array, baseFx,easing,ajaxUtil, dom, domAttr, domClass, domStyle, on, query, topic, 
    		BaseWidget,LandManageAndControlReportWidget,DrawGeometryWidget,SpecialDataReportWidget,
    		FeatureLayer,InfoTemplate,ContentPane, template) {
        return declare([BaseWidget], {
        	templateString: template,
            map: null,
            lefttabCon:null,
            treenode:null,
            constructor: function (args) {
                this.map = args.map;
                this.lefttabCon  = args.lefttabCon;
               
            },

            postCreate: function () {
            	this.inherited(arguments);
 
            },

            startup: function () {
                this.inherited(arguments);
                this.init();
            },

 
            init:function(){
            	this.treenode = [];
            	this.ShowResourceLis();
            	
            },
            
    		ShowResourceLis:function() { 
    			
    			var zNodes =[
    			 			{ id:1, pId:0, name:"三旧专题 ", open:true},
    			 			{ id:11, pId:1, name:"旧厂房", open:true,dataobj:{lys:"5",lyid:"JCFFL"}},
    			 			{ id:12, pId:1, name:"旧城镇",dataobj:{lys:"4",lyid:"JCHFL"}},
    			 			{ id:13, pId:1, name:"旧村庄",dataobj:{lys:"3",lyid:"JCUFL"}},
    			 			//{ id:14, pId:1, name:"全三旧", dataobj:{lys:"1",lyid:"QSJFL"}},
    			 			{ id:2, pId:0, name:"招拍挂",dataobj:{lys:"0",lyid:"ZPGFL"}},
    			 			{ id:3, pId:0, name:"留用地",dataobj:{lys:"2",lyid:"LYDFL"}}
    			 		];			
    				var ztreeSetting = { check: { 
    					autoCheckTrigger : false,
    					chkboxType : {"Y": "ps", "N": "ps"},
    					chkStyle : "checkbox",
    					enable : true,
    					nocheckInherit : false,
    					chkDisabledInherit : false,
    					radioType : "level"},data:{simpleData:{enable:true}}, callback: { onCheck: lang.hitch(this,this.xxTreeNodeClick) } };	
    				$.fn.zTree.init($("#resourcetree"), ztreeSetting, zNodes);
    				var treeObj = $.fn.zTree.getZTreeObj("resourcetree"); 
    				treeObj.expandAll(true); 
    		},
    		xxTreeNodeClick:function(event, treeId, treeNode){
    			if(treeNode.id == 0){
    				return;
    			}
    			if(treeNode.isParent){	//如果为父节点，则选择全部的子节点
//    				var treeObj = $.fn.zTree.getZTreeObj("resourcetree"); 
//    				treeObj.expandNode(treeNode,!treeNode.open,true,true);
    				var nodechildren = treeNode.children;
    				for(var i=0;i<nodechildren.length;i++){
    					if(nodechildren[i].checked){
    						this.addFeatureLayer(nodechildren[i].dataobj);
    					}else{
    						this.removeFeatureLayer(nodechildren[i].dataobj);
    					}
    					
    				}
    			}else{
    				if(treeNode.checked){
						this.addFeatureLayer(treeNode.dataobj);
					}else{
						this.removeFeatureLayer(treeNode.dataobj);
					}
    			}
    			
    		},
    		removeFeatureLayer:function(dataobj){
    			var layer = this.map.getLayer(dataobj.lyid);
    			if(layer){
    				this.map.removeLayer(layer);
    			}
    			
    		},
    		addFeatureLayer:function(dataobj){
    			var leatureLayer = new FeatureLayer(mapinfoTemplate[dataobj.lyid].layerurl, {
            		id:dataobj.lyid,
            		mode: FeatureLayer.MODE_SNAPSHOT,
            		outFields: ["*"],
            		infoTemplate: new InfoTemplate(mapinfoTemplate[dataobj.lyid].template)
            	});

            	//this.LandFeatureLayer .setRenderer(renderer);
            	this.map.addLayer(leatureLayer ,0);
            	on(leatureLayer ,"click", lang.hitch(this,function(evt) {
            		topic.publish("MapLandClickHandler",evt);
            	}));
    		},
            destroy: function () {
                var widgets = this.widgets;
                for (var name in widgets) {
                    if (widgets[name]) {
                        widgets[name].destroy();
                    }
                }
                this.inherited(arguments);
            },
      
        });
    });